#include "theater.h"
void divideIntoGroups( )
{
    int customersToAddToGroups = getMV(numCustomers);
    int groupSize = 0;
    int temp=0;
    while(customersToAddToGroups > 0)
    {
        if(customersToAddToGroups < 5) 
            groupSize = rand()%customersToAddToGroups + 1;
        else
            groupSize = rand()%5 + 1; 
        customersToAddToGroups -= groupSize;
        setMV(numCustomersInGroup[temp] ,groupSize);
        temp++;
    }
    setMV(numGroups,temp);
    setMV(numGroupsRemaining ,temp);
}

void RT1LO1(){
    int result;
    Printf("rpctest 5 or 6:RT2LO1 is going to login on \n");  
    result=rpcLoginOn(1234);
    if(-1!=result)
        Printf("rpctest 5 or 6:login successed\n");
    else
        Printf("rpctest 5 or 6:login failed\n");
}


void initRPCVars()
{
    int result;
    int i;
    CreateLock(ticketClerkWorknoLock);
    createMV(ticketClerkWorkno);
 
    CreateLock(lCustomerLeft);

/*mv for customers*/
    createMV(numGroups);
    createMV(nextCustomerId);
    for (i = 0; i < MAX_GROUP; ++i)
    {
        lGroup[i]="___lGroup";
        number(lGroup[i],i,10,0);
        CreateLock(lGroup[i] );

         
        headCustomerInGroup[i] = "___headCustomerInGroup";
       number(headCustomerInGroup[i] ,i,10,0);
       createMV(headCustomerInGroup[i]);
       setMV(headCustomerInGroup[i],-1);

        cGroup[i]="___cGroup";
        number(cGroup[i],i,10,0);
        CreateCV(cGroup[i]);

        
        cGroupFood[i] = "___cGroupFood";
       number(cGroupFood[i],i,10,0);
        CreateCV(cGroupFood[i]);
        
        cGroupBathroom[i]="___cGroupBathroom";
        number(cGroupBathroom[i],i,10,0);
        CreateCV( cGroupBathroom[i]);
        
        answerBathroom[i]="___answerBathroom";
        number(answerBathroom[i],i,10,0);
        createMV(answerBathroom[i]);

        numCustomersInGroup[i]="___numCustomersInGroup";
        number(numCustomersInGroup[i],i,10,0);
        createMV(numCustomersInGroup[i]);

        groupAskForFood[i]="___groupAskForFood";
        number(groupAskForFood[i],i,10,0);
        createMV(groupAskForFood[i]);

        groupTicket[i]="___groupTicket";
        number(groupTicket[i],i,10,0);
        createMV(groupTicket[i]);

        answerFood[i]="___answerFood";
        number(answerFood[i],i,10,0);
        createMV(answerFood[i]);

        groupCheck[i]="___groupCheck";
        number(groupCheck[i],i,10,0);
        createMV(groupCheck[i]);

        groupSeat[i]="___groupSeat";
        number(groupSeat[i],i,10,0);
        createMV(groupSeat[i]);

        groupLeaveRoom[i]="___groupLeaveRoom";
        number(groupLeaveRoom[i],i,10,0);
        createMV(groupLeaveRoom[i]);

        groupLeaveTheater[i]="___groupLeaveTheater";
        number(groupLeaveTheater[i],i,10,0);
        createMV(groupLeaveTheater[i]);

        groupAskForBathroom[i]="___groupAskForBathroom";
      number(groupAskForBathroom[i],i,10,0);
        createMV(groupAskForBathroom[i]);

        numInGroupAskForBathroom[i]="___numInGroupAskForBathroom";
        number(numInGroupAskForBathroom[i],i,10,0);
        createMV(numInGroupAskForBathroom[i]);

        curCustomerNumInGroup[i]="___curCustomerNumInGroup";
        number(curCustomerNumInGroup[i],i,10,0);
        createMV(curCustomerNumInGroup[i]);

        groupFoodSum[i][0]="___groupFood0Sum";
       number(groupFoodSum[i][0],i,10,0);
        createMV(groupFoodSum[i][0]);

        groupFoodSum[i][1]="___groupFood1Sum";
        number(groupFoodSum[i][1],i,10,0);
        createMV(groupFoodSum[i][1]);

        
    }

/*create mv*/
    createMV(concessionClerkWorkNO);
    createMV(breakConcessionClerk);
    createMV(seatPos);
    createMV(ticketClerkWorkno);
    createMV(breakTicketClerk);
    createMV(stopTicketTaken);
    createMV(ticketTaken);
    createMV(currentCustomerSeated);
    createMV(customerLeft);
    createMV(tcNumber);
    createMV(ccNumber);
    createMV(totalAmount);
    createMV(totalTicketSold);
    createMV(totalTicketTaken);
    createMV(managerAskMTPlay);
    createMV(tickettakerWorkno);
    createMV(breakTicketTaker);
    createMV(numCustomers);

/*end create mv*/
     CreateLock(cutomerNoLock);
     CreateLock(lBuyTickets);
     CreateCV(cNoTicketClerk);
     CreateCV(cBreakTicketClerk);
      CreateCV(cNoConcessionClerk);
     CreateCV(cBreakConcessionClerk);
     CreateCV(cNoTicketTaker);
      CreateCV(cBreakTicketTaker);

    createMV(noTicketClerk);
    createMV(noConcessionClerk);
    createMV(noTicketTaker);
    createMV(totalTicketSold);
    createMV(ttNumber);
    createMV(numGroupsRemaining);
    /*lock define*/
    CreateLock(lAmount);
    CreateLock(lTicketSold);
    /*end */
    for (i = 0; i < MAX_TC; ++i)
    {
        tc[i].lock="___tclock";
        number(tc[i].lock,i,10,0);
        CreateLock(tc[i].lock);

        tc[i].lineCondition="___tclineCondition";
        number(tc[i].lineCondition,i,10,0);
        CreateCV(tc[i].lineCondition);

        tc[i].msgCondition="___tcmsgCondition";
        number(tc[i].msgCondition,i,10,0);
        /*Printf("tc[i].lock %d, tc[i].lineCondition %d, tc[i].msgCondition %d\n", tc[i].lock, tc[i].lineCondition, tc[i].msgCondition); */
        tc[i].ticketSum="___tcticketSum";
        number(tc[i].ticketSum,i,10,0);
        createMV(tc[i].ticketSum);

        tc[i].isBusy="___tcisBusy";
        number(tc[i].isBusy,i,10,0);
        createMV(tc[i].isBusy);
        setMV(tc[i].isBusy,true);
        
        tc[i].isBreak="___tcisBreak";
        number(tc[i].isBreak,i,10,0);
        createMV(tc[i].isBreak);

        tc[i].waitingSize="___tcwaitingSize";
        tc[i].waitingSize=number(tc[i].waitingSize,i,10,0);
        createMV(tc[i].waitingSize);
        
        tc[i].amount="___tcamount";
        tc[i].amount=number(tc[i].amount,i,10,0);
        createMV(tc[i].amount);

        tc[i].payment="___tcpayment";
        tc[i].payment=number(tc[i].payment,i,10,0);
        createMV(tc[i].payment);

        ticketClerkAmount[i]="___ticketClerkAmount";
        ticketClerkAmount[i]=number(ticketClerkAmount[i],i,10,0);
        createMV(ticketClerkAmount[i]);

        tc[i].employeeType="___ticketclerk";
    }
    /*init for the concession clerk*/
    CreateLock(concessionClerkWorknoLock);
    CreateLock(lBuyFood);
    
    for (i = 0; i < MAX_CC; ++i)
    {
        cc[i].lock="___cclock";
        cc[i].lock=number(cc[i].lock,i,10,0);
        CreateLock(cc[i].lock);

        cc[i].lineCondition="___cclineCondition";
        cc[i].lineCondition=number(cc[i].lineCondition,i,10,0);
        CreateCV(cc[i].lineCondition);
        
        cc[i].msgCondition="___ccmsgCondition";
        cc[i].msgCondition=number(cc[i].msgCondition,i,10,0);
        CreateCV(cc[i].msgCondition);

        
        cc[i].msgCondition="___ccmsgCondition";
        cc[i].msgCondition=number(cc[i].msgCondition,i,10,0);
        /*Printf("cc[i].lock %d, cc[i].lineCondition %d, cc[i].msgCondition %d\n", cc[i].lock, cc[i].lineCondition, cc[i].msgCondition); */
        cc[i].ticketSum="___ccticketSum";
        cc[i].ticketSum=number(cc[i].ticketSum,i,10,0);
        createMV(cc[i].ticketSum);

        cc[i].isBusy="___ccisBusy";
        cc[i].isBusy=number(cc[i].isBusy,i,10,0);
        createMV(cc[i].isBusy);
        setMV(cc[i].isBusy,true);
        
        cc[i].isBreak="___ccisBreak";
        cc[i].isBreak=number(cc[i].isBreak,i,10,0);
        createMV(cc[i].isBreak);

        cc[i].waitingSize="___ccwaitingSize";
        cc[i].waitingSize=number(cc[i].waitingSize,i,10,0);
        createMV(cc[i].waitingSize);
        
        cc[i].amount="___ccamount";
        cc[i].amount=number(cc[i].amount,i,10,0);
        createMV(cc[i].amount);

        cc[i].payment="___ccpayment";
        cc[i].payment=number(cc[i].payment,i,10,0);
        createMV(cc[i].payment);

        cc[i].food[0]="___ccfood[0]";
        cc[i].food[0]=number(cc[i].food[0],i,10,0);
        createMV(cc[i].food[0]);
        
        cc[i].food[1]="___ccfood[1]";
        cc[i].food[1]=number(cc[i].food[1],i,10,0);
        createMV(cc[i].food[1]);

        concessionClerkAmount[i]="___concessionClerkAmount";
        concessionClerkAmount[i]=number(concessionClerkAmount[i],i,10,0);
        createMV(concessionClerkAmount[i]);

        cc[i].employeeType="___concessionclerk";
    }
    /*init ticket taker*/
    CreateLock(lCheckTickets);
    CreateCV(cTicketTaken);
    CreateLock(lTicketTaken);
    CreateLock(tickettakerWorknoLock);
    for (i = 0; i < MAX_TT; ++i)
    {
        tt[i].lock="___ttlock";
        tt[i].lock=number(tt[i].lock,i,10,0);
        CreateLock(tt[i].lock);

        tt[i].lineCondition="___ttlineCondition";
        tt[i].lineCondition=number(tt[i].lineCondition,i,10,0);
        CreateCV(tt[i].lineCondition);
        
        tt[i].msgCondition="___ttmsgCondition";
        tt[i].msgCondition=number(tt[i].msgCondition,i,10,0);
        CreateCV(tt[i].msgCondition);

        
        tt[i].msgCondition="___ttmsgCondition";
        tt[i].msgCondition=number(tt[i].msgCondition,i,10,0);
        /*Printf("tt[i].lock %d, tt[i].lineCondition %d, tt[i].msgCondition %d\n", tt[i].lock, tt[i].lineCondition, tt[i].msgCondition); */
        tt[i].ticketSum="___ttticketSum";
        tt[i].ticketSum=number(tt[i].ticketSum,i,10,0);
        createMV(tt[i].ticketSum);

        tt[i].isBusy="___ttisBusy";
        tt[i].isBusy=number(tt[i].isBusy,i,10,0);
        createMV(tt[i].isBusy);
        setMV(tt[i].isBusy,true);
        
        tt[i].isBreak="___ttisBreak";
        tt[i].isBreak=number(tt[i].isBreak,i,10,0);
        createMV(tt[i].isBreak);

        tt[i].waitingSize="___ttwaitingSize";
        tt[i].waitingSize=number(tt[i].waitingSize,i,10,0);
        createMV(tt[i].waitingSize);
        
        tt[i].amount="___ttamount";
        tt[i].amount=number(tt[i].amount,i,10,0);
        createMV(tt[i].amount);

        tt[i].payment="___ttpayment";
        tt[i].payment=number(tt[i].payment,i,10,0);
        createMV(tt[i].payment);

        tt[i].employeeType="___tickettaker";
    }
    for(i=0;i<MAX_SEAT;i++){
        seatState[i]="___seatState";
        seatState[i]=number(seatState[i],i,10,0);
        createMV(seatState[i]);
    }

    for(i=0;i<MAX_GROUPSIZE;i++){
        seatLocation[i]="___seatLocation";
        seatLocation[i]=number(seatLocation[i],i,10,0);
        createMV(seatLocation[i]);
    }
     CreateLock(lFindSeats);
    /*int for the movie technician*/
    CreateLock(lStopMovie);
    CreateLock(lMovieState);

    createMV(movieState);
    setMV(movieState,1);

    CreateLock(managerAskMTplaylock);
    CreateLock(currentCustomerSeatedlock);
    
    mt.lock="mtlock";
    CreateLock(mt.lock);
    
    mt.lineCondition="mtlineCondition";
    CreateCV(mt.lineCondition);
    
    mt.msgCondition="mtmsgCondition";
    CreateCV(mt.msgCondition);
    
    
    mt.msgCondition="mtmsgCondition";
    /*Printf("mt.lock %d, mt.lineCondition %d, mt.msgCondition %d\n", mt.lock, mt.lineCondition, mt.msgCondition); */
    mt.ticketSum="mtticketSum";
    createMV(mt.ticketSum);
    
    mt.isBusy="mtisBusy";
    createMV(mt.isBusy);
    setMV(mt.isBusy,true);
    
    mt.isBreak="mtisBreak";;
    createMV(mt.isBreak);
    
    mt.waitingSize="mtwaitingSize";
    createMV(mt.waitingSize);
    
    mt.amount="mtamount";
    createMV(mt.amount);
    
    mt.payment="mtpayment";
    createMV(mt.payment);

    mr.msgCondition="mrmsgCondition";
    CreateCV(mr.msgCondition);

    mr.employeeType="manager";


}

void initTheater()
{
    int i=0;
    int inputValue = 0;
    RT1LO1();

    initRPCVars();

    setMV(numCustomers , 40);
    setMV(tcNumber, 5);
    setMV(ccNumber ,5);
    setMV(ttNumber, 3);

    divideIntoGroups();
    Exit(0);

}

